/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 *//*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package poker_sim;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @author Rob
 */
public class DeckFactory {
    
    Deck _deck;
    BufferedImage CardImages;
    int LastX = 949;
    int LastY = 391;
    int CurrentX = 1;
    int CurrentY = 1;
    int IncreamentX = 73;
    int IncreamentY = 98;
    
    public DeckFactory(Deck _Deck)
    {
        //Initialize deck
        _deck = _Deck;
        Initialize();    
        //Create deck
            //create cards
            //add image per card
        //shuffle deck
        
        
    }
    public Deck getDeck()
    {
        return _deck;
    }
    
    public void Initialize() 
    {
        try {
            CardImages = ImageIO.read(_deck.pkr);
        } catch (IOException ex) {
            Logger.getLogger(Deck.class.getName()).log(Level.SEVERE, null, ex);
        }
        createDeck();
    }
    public void createDeck()
    {
        
        _deck.Size = 52;
        //Create deck
        int uniqueid = 0;
        CardType _Type = CardType.DEFAULT;
        String _CardName = "0";
        Stack _Deck = new Stack();
        for(int pos = 1; pos < 5; pos++)
        {
            
            switch(pos)
            {
                case(1):
                {
                    _Type = CardType.Clubs;
                    break;
                }
                case(2):
                {
                    _Type = CardType.Spades;
                    break;
                }
                case(3):
                {
                    _Type = CardType.Hearts;
                    break;
                }
                case(4):
                {
                    _Type = CardType.Diamonds;
                    break;
                }
                
            }
            for(int i = 1; i < 14; i++)
            {
                switch(i)
                {
                    case(1):
                    {
                        _CardName = "2";
                        break;
                    }
                    case(2):
                    {
                        _CardName = "3";
                        break;
                    }
                    case(3):
                    {
                        _CardName = "4";
                        break;
                    }
                    case(4):
                    {
                        _CardName = "5";
                        break;
                    }
                    case(5):
                    {
                        _CardName = "6";
                        break;
                    }
                    case(6):
                    {
                        _CardName = "7";
                        break;
                    }
                    case(7):
                    {
                        _CardName = "8";
                        break;
                    }
                    case(8):
                    {
                        _CardName = "9";
                        break;
                    }
                    case(9):
                    {
                        _CardName = "10";
                        break;
                    }
                    case(10):
                    {
                        _CardName = "Jack";
                        break;
                    }
                    case(11):
                    {
                        _CardName = "Queen";
                        break;
                    }
                    case(12):
                    {
                        _CardName = "King";
                        break;
                    }
                    case(13):
                    {
                        _CardName = "Ace";
                        break;
                    }
                }
                //System.out.println("CurrentX " + CurrentX + " CurrentY " + CurrentY + " Width " + (IncreamentX - 2) + " Heigth " + (IncreamentY - 2));
                _Deck.add(new Card(uniqueid,_CardName, _Type,CardImages.getSubimage(CurrentX, CurrentY, IncreamentX - 2, IncreamentY - 2)));
                //System.out.println(_CardName + " " + _Type.TypeName);
                uniqueid++;
                CurrentX += IncreamentX;
            }
            CurrentX = 1;
            CurrentY += IncreamentY;
            
        }
        shuffleDeck(_Deck);
    }

    public void shuffleDeck(Stack _Deck)
    {
        int itor = 0;
        while(_Deck.size() != 0)
        {
            Random r = new Random();
            int Randomnr = r.nextInt(51);
            
            Randomnr = Randomnr % (_Deck.size());
            _deck.deck[itor] = (Card)_Deck.get(Randomnr);
            System.out.println(itor + " "+_deck.deck[itor].CardName + " " + _deck.deck[itor].Type.TypeName);
            _Deck.remove(Randomnr);
            itor++;

        }

    }
    
}
